#include "m_kni.h"

int config_network_if(uint16_t port_id, uint8_t if_up){
    if(!rte_eth_dev_is_valid_port(port_id)){
        return -EINVAL;
    }
    int ret = 0;
    if(if_up){
        rte_eth_dev_stop(port_id);
        ret = rte_eth_dev_start(port_id);
    }else{
        rte_eth_dev_stop(port_id);
    }
    if(ret < 0){
        printf(" failed to start port  \n");
    }
    return 0;
}

void alloc_kni(struct rte_mempool * m_pool, struct rte_kni ** kni_hanlder){
    /**
     *  1. rte_mempool
     *  2. rte_kni_conf
     *  3. rte_kni_ops
     * */
    struct rte_kni_conf conf;
    memset(&conf, 0, sizeof(conf));
    snprintf(conf.name, RTE_KNI_NAMESIZE, "vEth%u", 0);
    conf.group_id = 0;
    conf.mbuf_size = MAX_PACKET_SIZE;
    rte_eth_macaddr_get(0, (struct rte_ether_addr *)conf.mac_addr);
    rte_eth_dev_get_mtu(0, &conf.mtu);

    print_eth_address("alloc kni", (struct rte_ether_addr *)conf.mac_addr);

    struct rte_kni_ops ops;
    memset(&ops, 0, sizeof(ops));
    ops.port_id = 0;
    ops.config_network_if = config_network_if;

    *kni_hanlder = rte_kni_alloc(m_pool, &conf, &ops);
    if (!(*kni_hanlder)) {
        rte_exit(EXIT_FAILURE, "Failed to create kni for port : %d\n", 0);
    }
}